package com.szqhyxj.youxiangju.biz.merchantHome.my;

import com.szqhyxj.youxiangju.BaseConsumer;
import com.szqhyxj.youxiangju.biz.core.TokenManager;
import com.szqhyxj.youxiangju.biz.core.UserInfo;
import com.szqhyxj.youxiangju.biz.core.UserInfoManger;
import com.szqhyxj.youxiangju.biz.core.UserInfoModel;
import com.szqhyxj.youxiangju.sys.BaseResult;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class MyPresenter implements MyContract.Presenter {

    private MyContract.View view;

    MyPresenter(MyContract.View view) {
        this.view = view;
    }

    @Override
    public void init() {
        List<MyModel.IconItem> iconItems = MyModel.getIconItems();
        view.showMyFuncItems(iconItems);
        if (!TokenManager.INSTANCE.online()) {
            view.showLogin();
        } else {
            view.showMy();
            getUserInfo();
            view.showUserInfo(UserInfoManger.INSTANCE.getUser());
            if (UserInfoManger.INSTANCE.getUser().getUserType() == 5 || UserInfoManger.INSTANCE.getUser().getUserType() == 4) {
                view.showPartner();

            }

                else{
                view.enshowPartner();
            }
            if (UserInfoManger.INSTANCE.getUser().getOnlineVip() == 0) {
                view.enshowVip();
            } else {
                view.showVip();
            }
            if (UserInfoManger.INSTANCE.getUser().getRealNameAuthStatus() == 0) {
                view.showCertification();
            }
            if (UserInfoManger.INSTANCE.getUser().getRealNameAuthStatus() == 1) {
                view.enshowCertification();
            }
            view.showBalance(UserInfoManger.INSTANCE.getUser().getBalance() + "");
        }

    }

    @Override
    public void getwarrant() {
        MyModel.getwarrant()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseConsumer<MyModel.WarrantBean>() {
                    @Override
                    protected void error(String code, String msg) {
                        view.showToast(msg);
                    }

                    @Override
                    protected void successAccept(MyModel.WarrantBean warrantBean) {
                        view.showPower(warrantBean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showToast("未登录");
                    }
                });
    }

    @Override
    public void monthProfit() {
        MyModel.monthProfit()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<String>>() {
                    @Override
                    public void accept(BaseResult<String> monthProfit) throws Exception {
                        if (monthProfit.businessLogicSuccess()) {
                            view.showMonthProfit(monthProfit.getData());
                        } else {
                            view.showToast(monthProfit.getMessage() + "");

                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                        view.showToast(throwable.getMessage());
                    }
                });
    }

    public void getUserInfo() {

        UserInfoModel.loadUserInfo()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(
                        new Consumer<BaseResult<UserInfo>>() {
                            @Override
                            public void accept(BaseResult<UserInfo> baseResult) throws Exception {
                                if (baseResult.businessLogicSuccess()) {
                                    UserInfoManger.INSTANCE.clear();
                                    UserInfoManger.INSTANCE.init(baseResult.getData());
                                } else {
                                    view.showToast(baseResult.getMessage());
                                }
                            }
                        },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                view.showToast(throwable.getMessage());
                            }
                        });
    }


}